nframe = size(neuron.C_raw, 2);
ncell = size(neuron.C_raw, 1);
tlen = nframe / Fs;
bstates = zeros(1, nframe);
zerostates = zeros(1, nframe);
nstate = length(events);
tseg = [0 312.4 841.2 1227.1];
for i = 1:nstate
    event_now = events{i};
    tRise = event_now(:,1); tDur = event_now(:,2) - tRise;
    flag = revertTTL2bin(tRise, tDur, Fs, tlen);
    bstates(flag) = i;
    serial = (1:nframe)'/Fs;
    flagzero = serial>tseg(i) & serial<tseg(i+1) & ~flag;
    zerostates(flagzero) = i; 
end
tic
aucs = zeros(ncell, nstate);
pvals = zeros(ncell, nstate);
rankps = zeros(ncell, nstate);
for istate = 1:nstate
    zerostate = zerostates==istate;
%     zerostate = zerostates>0;
    behavioralstate = bstates==istate;
    for icell = 1:ncell
        timeseries = neuron.C_raw(icell,:);
        [aucs(icell, istate), pvals(icell, istate), ~] = auROC_withStates_cxf(timeseries,behavioralstate,zerostate,100,5000);
        rankps(icell, istate) = ranksum(timeseries(behavioralstate), timeseries(zerostate));
    end
end
toc


% tic
[a, b, ~] = auROC_withStates_cxf(timeseries,behavioralstate,zerostate,100,5000)
% toc
% tic
% [a, b, ~] = auROC_withStates(timeseries,behavioralstate,zerostate,100,5000)
% toc

icell =4;
istate = 1;
zerostate = zerostates==istate;
% zerostate = zerostates>0;
behavioralstate = bstates==istate;
timeseries = neuron.C_raw(icell,:);
[a, b, ~] = auROC_withStates_cxf(timeseries,behavioralstate,zerostate,100,5000);
